package org.pine.dictionary.bean;

import cn.hutool.core.util.StrUtil;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * <p>
 * 日志ID
 * </p>
 *
 * @author pine
 * @since 2024-05-16
 */
public class LogId {
    public static final String KEY_MDC = "logId";

    private LogId() {
    }


    public static void put(String logId) {
        MDC.put(KEY_MDC, logId);
    }

    public static String get() {
        String logId = MDC.get(KEY_MDC);
        if (logId == null) {
            logId = generate();
            put(logId);
        }

        return logId;
    }

    public static void remove() {
        MDC.remove(KEY_MDC);
    }

    public static String generate() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace(StrUtil.DASHED, StrUtil.EMPTY);
    }

    public static String concat(Object suffix) {
        return get() + StrUtil.DASHED + suffix;
    }
}
